package com.zoe.app.config;

import com.zoe.app.intercept.DynamicIntercept;
import com.zoe.app.intercept.NewAsyncInterceptor;
import com.zoe.app.intercept.ZoeAsyncInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Zoe.Zhang
 * @date 2021/10/10
 * @description
 */
@Configuration
public class DynamicWebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private DynamicIntercept dynamicIntercept;

    @Autowired
    private ZoeAsyncInterceptor zoeAsyncInterceptor;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    NewAsyncInterceptor newAsyncInterceptor() {
        return new NewAsyncInterceptor();
    }
    /**
     * Add Spring MVC lifecycle interceptors for pre- and post-processing of
     * controller method invocations and resource handler requests.
     * Interceptors can be registered to apply to all requests or be limited
     * to a subset of URL patterns.
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        NewAsyncInterceptor newAsyncInterceptor = newAsyncInterceptor();
        NewAsyncInterceptor bean = applicationContext.getBean(NewAsyncInterceptor.class);
        System.out.println(newAsyncInterceptor == bean);
        registry.addInterceptor(dynamicIntercept)
                .addPathPatterns("/**")
                .excludePathPatterns("/hello");
        registry.addInterceptor(zoeAsyncInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/hello");
        System.out.println(registry);
    }
}
